<script>
    $(document).ready(function () {
        //添加
        $("#add-sub").click(function () {
            var host_name = $("#host-name").val();
            var ip = $("#ip").val();
            var inner_ip = $("#inner-ip").val();
            var port = $("#port").val();
            var label_id = $("#asset-label").val();
            var idc_id = $("#asset-idc").val();
            var lguser_id = $("#asset-lguser").val();
            $.ajax({
                type:"POST",
                async:false,
                url:"/cmdb/addasset/",
                data:{"host_name":host_name,"ip":ip,"inner_ip":inner_ip,"label_id":label_id,"idc_id":idc_id,"lguser_id":lguser_id,"port":port},
                dataType : "json",
                success:function (data) {
                    $("#addModal").modal('hide');
                    spop({
                        template: data,
                        style: 'success',
                        autoclose: 2000
                    });
                    setTimeout("location.reload()",2100);
                },
                error:function (data) {
                    spop({
                        template:data.responseText,
                        style: 'error',
                        autoclose: 2000
                    });
                }
            })
        });

        //获取修改内容
        $("td>a[name='edit-asset']").click(function () {
            var id = $(this).attr("edit_id");
            $.ajax({
                type: "POST",
                async: false,
                url: "/cmdb/idasset/",
                data:{"id":id },
                dataType: "json",
                success:function (data) {
                    $("#edit-host-name").val(data.HostName);
                    $("#edit-ip").val(data.Ip);
                    $("#edit-inner-ip").val(data.InnerIp);
                    $("#edit-port").val(data.Port);
                    $("#edit-asset-label").val(data.LabelID);
                    $("#edit-asset-idc").val(data.IdcID);
                    $("#edit-asset-lguser").val(data.LgUserID);
                    $("#edit-sub").attr("edit_id",data.ID);
                    $("#editModal").modal("show");
                },
                error:function (data) {
                    spop({
                        template:data.responseText,
                        style: 'error',
                        autoclose: 2000
                    });
                }
            });
        });

        //提交修改内容
        $("#edit-sub").click(function () {
            var id = $(this).attr("edit_id");
            var host_name = $("#edit-host-name").val();
            var ip = $("#edit-ip").val();
            var inner_ip = $("#edit-inner-ip").val();
            var port = $("#edit-port").val();
            var label_id = $("#edit-asset-label").val();
            var idc_id = $("#edit-asset-idc").val();
            var lguser_id = $("#edit-asset-lguser").val();
            $.ajax({
                type:"PUT",
                async:false,
                url:"/cmdb/editasset/",
                data:{"id":id,"host_name":host_name,"ip":ip,"inner_ip":inner_ip,"label_id":label_id,"idc_id":idc_id,"lguser_id":lguser_id,"port":port},
                dataType : "json",
                success:function (data) {
                    $("#editModal").modal('hide');
                    spop({
                        template: data,
                        style: 'success',
                        autoclose: 2000
                    });
                    setTimeout("location.reload()",2100);
                },
                error:function (data) {
                    spop({
                        template:data.responseText,
                        style: 'error',
                        autoclose: 2000
                    });
                }
            })
        });

        //删除
        $("td>a[name='del-asset']").click(function () {
            var id = $(this).attr('del_id');
            var statu = confirm("是否确认删除!");
            if (statu==true)
            {
                $.ajax({
                    type: "post",
                    async:false,
                    url: "/cmdb/delasset/",
                    data: {"id":id},
                    dataType : "json",
                    success: function(data) {
                        spop({
                            template:data,
                            style: 'warning',
                            autoclose: 2000
                        });
                        setTimeout("location.reload()",2100);
                    },
                    error:function (data) {
                        spop({
                            template:data.responseText,
                            style: 'error',
                            autoclose: 2000
                        });
                    }
                });
            }
        });

        //搜索角色
        $("#search-key").bind('change',function () {
            var key = $(this).val();
            if (key != "" ) {
                var url = "/cmdb/searchasset/" + key + "/";
            }else{
                var url = "/cmdb/asset/";
            }
            $("#search-a").attr("href", url);
        });

        //获取修改内容
        $("td>a[name='info-asset']").click(function () {
            var id = $(this).attr("info_id");
            $.ajax({
                type: "POST",
                async: false,
                url: "/cmdb/detailasset/",
                data:{"id":id },
                dataType: "json",
                success:function (data) {
                    //////////////基础信息//////////////

                    $("#infoModalLabel").empty();
                    $("#infoModalLabel").append(data.Ip," 详细信息");

                    $("#detail-id").empty();
                    $("#detail-id").append(data.ID);



                    $("#detail-ip").empty();
                    $("#detail-ip").append(data.Ip);

                    $("#detail-os-type").empty();
                    $("#detail-os-type").append(data.AssetOs.AssetType);

                    $("#detail-status").empty();
                    $("#detail-status").append(Number(data.Status));

                    $("#detail-label").empty();
                    $("#detail-label").append(data.Label.LabelName);

                    $("#detail-idc").empty();
                    $("#detail-idc").append(data.Idc.IdcName);

                    ////////////////系统信息//////////////////

                    $("#detail-hostname").empty();
                    $("#detail-hostname").append(data.HostName);


                    $("#detail-inner-ip").empty();
                    $("#detail-inner-ip").append(data.InnerIp);

                    $("#detail-host-arp").empty();
                    $("#detail-host-arp").append(data.AssetOs.HostArp);

                    $("#detail-product").empty();
                    $("#detail-product").append(data.AssetOs.ProductName);


                    $("#detail-lguser").empty();
                    $("#detail-lguser").append(data.LgUser.UserName);


                    $("#detail-auth-method").empty();
                    $("#detail-auth-method").append(data.LgUser.AuthMethod);


                    $("#detail-port").empty();
                    $("#detail-port").append(data.Port);


                    $("#detail-os-version").empty();
                    $("#detail-os-version").append(data.AssetOs.OsVersion);


                    $("#detail-ker-version").empty();
                    $("#detail-ker-version").append(data.AssetOs.KernelVersion);


                    $("#detail-cpu").empty();
                    $("#detail-cpu").append(data.AssetOs.CPUModel);

                    $("#detail-cpu-count").empty();
                    $("#detail-cpu-count").append(data.AssetOs.CPUCoreCount);

                    $("#detail-memory").empty();
                    $("#detail-memory").append(data.AssetOs.MemorySize," GB");
                    //////////////////网卡信息/////////////////////

                    var head_html = `
                            <div class="col-sm-2" style="padding-bottom: 5px">
                                <label>网卡</label>
                            </div>
                            <div class="col-sm-2" style="padding-bottom: 5px">
                                <label>IP</label>
                            </div>
                            <div class="col-sm-2" style="padding-bottom: 5px">
                                <label>MAC</label>
                            </div>
                            <div class="col-sm-2" style="padding-bottom: 5px">
                                <label>MASK</label>
                            </div>
                            <div class="col-sm-2" style="padding-bottom: 5px">
                                <label>MTU</label>
                            </div>
                            <div class="col-sm-2" style="padding-bottom: 5px">
                                <label>类型</label>
                            </div>`;
                    var boday_html = '';
                    for (i=0;i<data.AssetNic.length;i++){
                        boday_html += "<div class=\"col-sm-2\" style=\"padding-bottom: 5px\"><div class=\"skillbar\">"+data.AssetNic[i].NicName+"</div></div>"
                            + "<div class=\"col-sm-2\" style=\"padding-bottom: 5px\"><div class=\"skillbar\">"+data.AssetNic[i].IpAddr+"</div></div>"
                            + "<div class=\"col-sm-2\" style=\"padding-bottom: 5px\"><div class=\"skillbar\">"+data.AssetNic[i].Mac+"</div></div>"
                            + "<div class=\"col-sm-2\" style=\"padding-bottom: 5px\"><div class=\"skillbar\">"+data.AssetNic[i].NetMask+"</div></div>"
                            + "<div class=\"col-sm-2\" style=\"padding-bottom: 5px\"><div class=\"skillbar\">"+data.AssetNic[i].Mtu+"</div></div>"
                            + "<div class=\"col-sm-2\" style=\"padding-bottom: 5px\"><div class=\"skillbar\">"+data.AssetNic[i].NicModel+"</div></div>";
                    }

                    var new_html = head_html + boday_html;

                    $("#detail-nic").empty();
                    $("#detail-nic").append(new_html);


                    /////////////////////分区信息//////////////////////


                    var part_head_html = `
                            <div class="my-sm-3" style="padding-bottom: 5px">
                                <label>分区</label>
                            </div>
                            <div class="my-sm-3" style="padding-bottom: 5px">
                                <label>挂载</label>
                            </div>
                            <div class="my-sm-3" style="padding-bottom: 5px">
                                <label>类型</label>
                            </div>
                            <div class="my-sm-3" style="padding-bottom: 5px">
                                <label>总大小</label>
                            </div>
                            <div class="my-sm-3" style="padding-bottom: 5px">
                                <label>可用大小</label>
                            </div>`;
                    var part_boday_html = '';
                    for (i=0;i<data.AssetPartitions.length;i++){
                        part_boday_html += "<div class=\"my-sm-3\" style=\"padding-bottom: 5px\"><div class=\"skillbar\">"+data.AssetPartitions[i].Partition+"</div></div>"
                            + "<div class=\"my-sm-3\" style=\"padding-bottom: 5px\"><div class=\"skillbar\">"+data.AssetPartitions[i].Mount+"</div></div>"
                            + "<div class=\"my-sm-3\" style=\"padding-bottom: 5px\"><div class=\"skillbar\">"+data.AssetPartitions[i].FsType+"</div></div>"
                            + "<div class=\"my-sm-3\" style=\"padding-bottom: 5px\"><div class=\"skillbar\">"+data.AssetPartitions[i].SizeTotal+" MB</div></div>"
                            + "<div class=\"my-sm-3\" style=\"padding-bottom: 5px\"><div class=\"skillbar\">"+data.AssetPartitions[i].SizeAvaileble+" MB</div></div>";
                    }

                    var part_new_html = part_head_html + part_boday_html;

                    $("#detail-partition").empty();
                    $("#detail-partition").append(part_new_html);


                    $("#infoModal").modal("show");
                },
                error:function (data) {
                    spop({
                        template:data.responseText,
                        style: 'error',
                        autoclose: 2000
                    });
                }
            });

        });


        $("a[name='con-asset']").click(function () {
            var  id = $(this).attr("con_id");
            $.ajax({
                type: "POST",
                async: false,
                url: "/cmdb/connect/",
                data:{"id":id },
                dataType: "json",
                success:function (data) {
                    spop({
                        template:data,
                        style: 'success',
                        autoclose: 2000
                    });
                    setTimeout("location.reload()",2100);
                },
                error:function (data) {
                    spop({
                        template:data.responseText,
                        style: 'error',
                        autoclose: 2000
                    });
                }
            });
        });


        $("#asset-sync").click(function () {
            $.ajax({
                type: "POST",
                async: false,
                url: "/cmdb/syncasset/",
                data:{},
                dataType: "json",
                success:function (data) {
                    spop({
                        template:data,
                        style: 'success',
                        autoclose: 2000
                    });
                },
                error:function (data) {
                    spop({
                        template:data.responseText,
                        style: 'error',
                        autoclose: 2000
                    });
                }
            });
        });

    })
</script>
